<?php
require_once dirname(__FILE__)."/Yaml/sfYaml.php";

class Framework
{
  static public $SETTINGS = null;
  static public $BASEDIR  = null;
  
  /**
   * Initializes the framework. Call this once in your functions.php file.
   *
   * @return void
   * @author Carlos
   */
  static public function initialize($config = array())
  {
    self::$SETTINGS = sfYaml::load(dirname(__FILE__).'/Framework.yml');
    self::$BASEDIR  = $BASEDIR = dirname(__FILE__);
    
    // Autoload
    ini_set('unserialize_callback_func', 'spl_autoload_call');
    spl_autoload_register(array(new self, 'autoload'));
    
    // Required PHP Files
    foreach (self::$SETTINGS['required'] as $file)
      require_once "$BASEDIR/$file.php";
    if (isset($config['required']))
    {
      foreach ((array) $config['required'] as $file)
        require_once "$file.php";
    }
    
    // Templates
    $tpl = array();
    foreach (self::$SETTINGS['templates'] as $path)
      if (is_dir($tmp = "$BASEDIR/$path"))
        $tpl[] = $tmp;
    self::$SETTINGS['templates'] = $tpl;
    
    if (isset($config['templates']))
      self::addTemplatesPath($config['templates']);
  }
  
  /**
   * Framework classes autoloading
   *
   * @package default
   * @author Carlos
   */
  static public function autoload($class)
  {
    $p = strpos($class, '_');
    $b = dirname(__FILE__);
    
    if ((false === $p) && is_file($file = "$b/$class.php"))
    {
      require_once $file;
      return true;
    }
    elseif (in_array(substr($class, 0, $p), self::$SETTINGS['autoload']))
    {
      require dirname(__FILE__).'/'.str_replace('_', '/', $class).'.php';
      return true;
    }
    
    return false;
  }
  
  /**
   * Returns a Twig environment with Filesystem loader and registered paths
   * get from Framework settings and from the templatepath parameter.
   *
   * @param string $templatepath (optional)
   * @return Twig_Environment
   * @author Carlos
   */
  static public function Twig($templatepath = null)
  {
    $lodr = new Twig_Loader_Filesystem (
      array_merge(self::$SETTINGS['templates'], (array) $templatepath)
      );
    return new Twig_Environment($lodr, array('cache' => false));
  }
  
  /**
   * Register template paths in Framework settings
   *
   * @param string $paths 
   * @return void
   * @author Carlos
   */
  static public function addTemplatesPath($paths = null)
  {
    foreach ((array) $paths as $path)
      if (is_dir($path))
        self::$SETTINGS['templates'][] = $path;
  }
  
  static public function registerSetting($setting, $value)
  {
    self::$SETTINGS[$setting] = $value;
  }
  
  /**
   * Disables the Wordpress Update warning for non administrator users or all if
   * specified.
   *
   * @param boolean $all 
   * @return void
   * @author Carlos
   */
  static public function disableUpdateWarning($all = false)
  {
    if ($all || !current_user_can('edit_users')) {
      add_action('init', create_function('$a', "remove_action('init', 'wp_version_check');"), 2);
      add_filter('pre_option_update_core', create_function('$a', "return null;"));
    }
  }
}
